#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include "Mutex.h"
#include "Cond.h"
#include "Thread.h"
#include "Log.h"
#include <memory>

namespace ThreadPoolMoudle
{
    using namespace lxp;
    using namespace ThreadModule;
    using namespace LogMudule;
    using namespace CondModule;

    const static int defaultnum = 5;

    using thread_t = std::shared_ptr<Thread>;
    void DefaultTest()
    {
        while (true)
        {
            LOG(LogLevel::DEBUG) << "我是一个测试方法";
            sleep(1);
        }
    }
    template <class T>
    class ThreadPool
    {
    private:
        bool IsEmpty()
        {
            return _taskq.empty();
        }
        void HandlerTask(std::string name)
        {
            LOG(LogLevel::INFO) << "线程" << name << ",进入handertask的逻辑";
            while (1)
            {
                T t;
                {
                    LockGuard lockguard(_lock);
                    while (IsEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }
                    if (IsEmpty() && !_isrunning)
                    {
                        break;
                    }
                    t = _taskq.front();
                    _taskq.pop();
                }
                t(name);
            }
            LOG(LogLevel::INFO) << "线程" << name << "退出";
        }

    public:
        ThreadPool(int num = defaultnum)
            : _num(num), _wait_num(num), _isrunning(false)
        {
            for (int i = 0; i < _num; i++)
            {
                _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1)));
                LOG(LogLevel::INFO) << "构建线程" << _threads.back()->Name() << "对象成功";
            }
        }
        void Equeue(T &&in)
        {
            LockGuard lockguard(_lock);
            if (!_isrunning)
            {
                return;
            }
            _taskq.push(std::move(in));
            if (_wait_num > 0)
            {
                _cond.Notify();
            }
        }
        void Start()
        {
            if (_isrunning)
            {
                return;
            }
            _isrunning = true;
            for (auto &thread_ptr : _threads)
            {
                thread_ptr->Start();
                LOG(LogLevel::INFO) << "启动进程" << thread_ptr->Name() << "...成功";
            }
        }
        void Wait()
        {
            for (auto &thread_ptr : _threads)
            {
                thread_ptr->Join();
                LOG(LogLevel::INFO) << "回收进程" << thread_ptr->Name() << "...成功";
            }
        }
        void Stop()
        {
            LockGuard lockgurad(_lock);
            if (_isrunning)
            {
                _isrunning = false;
                if (_wait_num > 0)
                {
                    _cond.NotifyAll();
                }
            }
        }
        ~ThreadPool()
        {
        }

    private:
        std::vector<thread_t> _threads;
        int _num;
        int _wait_num;
        std::queue<T> _taskq;

        Mutex _lock;
        Cond _cond;

        bool _isrunning;
    };
}